from openpolitics.models import *
from django.contrib import admin
from django.contrib.contenttypes import generic

class DataInline(generic.GenericTabularInline):
    model=DataItem
    ct_field_name = 'content_type'
    id_field_name = 'object_id'

class TermInline(admin.TabularInline):
    model = Term

class InstToInstRelationInline(admin.TabularInline):
    model=InstToInstRelation
    fk_name='institution'

class PosToPosRelationInline(admin.TabularInline):
    model=PosToPosRelation
    fk_name='position'

class NationalityInline(admin.TabularInline):
    model=Nationality

class PersonAdminWithData(admin.ModelAdmin):
    inlines = [TermInline, NationalityInline, DataInline]
    prepopulated_fields = {'slug':['first_name', 'last_name']}

class InstitutionAdmin(admin.ModelAdmin):
    inlines = [InstToInstRelationInline, DataInline]
    prepopulated_fields = {'slug':['shortname']}

class PositionAdmin(admin.ModelAdmin):
    inlines = [PosToPosRelationInline, DataInline]

class ElectionsAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug':['position', 'date']}


admin.site.register(InstitutionType)
admin.site.register(Institution, InstitutionAdmin)
admin.site.register(State, InstitutionAdmin)
admin.site.register(PositionType)
admin.site.register(Position, PositionAdmin)
admin.site.register(Person, PersonAdminWithData)
admin.site.register(Term)
admin.site.register(Elections, ElectionsAdmin)
admin.site.register(PersonCandidate)
admin.site.register(InstitutionCandidate)
